package com.jpress.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.text.MessageFormat;
import java.util.ArrayList;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;

import com.jpress.configuration.ConfigurationManager;


public class EmailUtils {

	public enum TipoEmail{
		EMAIL_SIMPLES, EMAIL_HTML, EMAIL_ANEXO
	}

	public EmailUtils(){

	}

	public static void enviarEmailTexto(String assunto, String mensagem, ArrayList<String> destinatarios, ArrayList<String> copiasCarbono, ArrayList<String> copiasOcultas) throws EmailException{
		SimpleEmail email = (SimpleEmail) emailFactory(TipoEmail.EMAIL_SIMPLES);
		for (String destinatario : destinatarios) {
			email.addTo(destinatario);
		}
		if(copiasCarbono != null){
			for (String copiaCarbono : copiasCarbono) {
				email.addCc(copiaCarbono);
			}
		}
		if(copiasOcultas != null){
			for (String copiaOculta : copiasOcultas) {
				email.addBcc(copiaOculta);
			}
		}
		email.setSubject(assunto);
		email.setMsg(mensagem);
		email.send();
	}

	public static void enviarEmailHTML(String assunto, String textoMensagem, ArrayList<String> destinatarios, ArrayList<String> copiasCarbono, ArrayList<String> copiasOcultas, String arquivoHtml, Object[] argumentosHTML) throws EmailException, MalformedURLException{
		HtmlEmail email = (HtmlEmail) emailFactory(TipoEmail.EMAIL_HTML);
		email.setSubject(assunto);
		for (String destinatario : destinatarios) {
			email.addTo(destinatario);
		}
		if(copiasCarbono != null){
			for (String copiaCarbono : copiasCarbono) {
				email.addCc(copiaCarbono);
			}
		}
		if(copiasOcultas != null){
			for (String copiaOculta : copiasOcultas) {
				email.addBcc(copiaOculta);
			}
		}
		try {
			String html = MessageFormat.format(getMessageHTML(arquivoHtml), argumentosHTML);
			email.setHtmlMsg(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		if(textoMensagem != null){
			String texto = MessageFormat.format(textoMensagem, argumentosHTML);
			email.setTextMsg(texto);
		}
		email.send();
	}

	public void enviarEmailAnexo(){

	}

	public static Email emailFactory(TipoEmail tipo) throws EmailException{
		Email email;
		switch (tipo) {
		case EMAIL_SIMPLES: 
			email = new SimpleEmail();
			break;
		case EMAIL_HTML:
			email = new HtmlEmail();
			break;
		default:
			email = new SimpleEmail();
		}
		ConfigurationManager cm = ConfigurationManager.getInstance();
		email.setHostName(cm.getParametroAsString("email.hostname"));
		email.setFrom(cm.getParametroAsString("email.from_address"), cm.getParametroAsString("email.from_name"));
		email.setAuthentication(cm.getParametroAsString("email.auth_login"), cm.getParametroAsString("email.auth_senha"));
		email.setSmtpPort(cm.getParametroAsInt("email.smtp_port"));
		email.setSSL(cm.getParametroAsBoolean("email.security_ssl"));
		email.setTLS(cm.getParametroAsBoolean("email.security_tls"));
		return email;
	}
	
	public static String getMessageHTML(String mensagem) throws IOException {
		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext ex = fc.getExternalContext();
		InputStream in = ex.getResourceAsStream("/WEB-INF/classes/com/jpress/prompts/email/" + mensagem + ".html");
		return GeralUtils.convertStreamToString(in);
	}
}
